home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 125
/
Freelog_MarsAvril2015_No125.iso
/
Musique
/
Quod Libet
/
quodlibet-3.3.0-installer.exe
/
bin
/
urlparse.pyc
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2014-12-31
|
14KB
|
505 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.7)
'''Parse (absolute and relative) URLs.
urlparse module is based upon the following RFC specifications.
RFC 3986 (STD66): "Uniform Resource Identifiers" by T. Berners-Lee, R. Fielding
and L. Masinter, January 2005.
RFC 2732 : "Format for Literal IPv6 Addresses in URL\'s by R.Hinden, B.Carpenter
and L.Masinter, December 1999.
RFC 2396: "Uniform Resource Identifiers (URI)": Generic Syntax by T.
Berners-Lee, R. Fielding, and L. Masinter, August 1998.
RFC 2368: "The mailto URL scheme", by P.Hoffman , L Masinter, J. Zwinski, July 1998.
RFC 1808: "Relative Uniform Resource Locators", by R. Fielding, UC Irvine, June
1995.
RFC 1738: "Uniform Resource Locators (URL)" by T. Berners-Lee, L. Masinter, M.
McCahill, December 1994
RFC 3986 is considered the current standard and any future changes to
urlparse module should conform with it. The urlparse module is
currently not entirely compliant with this RFC due to defacto
scenarios for parsing, and for backward compatibility purposes, some
parsing quirks from older RFCs are retained. The testcases in
test_urlparse.py provides a good indicator of parsing behavior.
'''
import re
__all__ = [
'urlparse',
'urlunparse',
'urljoin',
'urldefrag',
'urlsplit',
'urlunsplit',
'parse_qs',
'parse_qsl']
uses_relative = [
'ftp',
'http',
'gopher',
'nntp',
'imap',
'wais',
'file',
'https',
'shttp',
'mms',
'prospero',
'rtsp',
'rtspu',
'',
'sftp',
'svn',
'svn+ssh']
uses_netloc = [
'ftp',
'http',
'gopher',
'nntp',
'telnet',
'imap',
'wais',
'file',
'mms',
'https',
'shttp',
'snews',
'prospero',
'rtsp',
'rtspu',
'rsync',
'',
'svn',
'svn+ssh',
'sftp',
'nfs',
'git',
'git+ssh']
uses_params = [
'ftp',
'hdl',
'prospero',
'http',
'imap',
'https',
'shttp',
'rtsp',
'rtspu',
'sip',
'sips',
'mms',
'',
'sftp',
'tel']
non_hierarchical = [
'gopher',
'hdl',
'mailto',
'news',
'telnet',
'wais',
'imap',
'snews',
'sip',
'sips']
uses_query = [
'http',
'wais',
'imap',
'https',
'shttp',
'mms',
'gopher',
'rtsp',
'rtspu',
'sip',
'sips',
'']
uses_fragment = [
'ftp',
'hdl',
'http',
'gopher',
'news',
'nntp',
'wais',
'https',
'shttp',
'snews',
'file',
'prospero',
'']
scheme_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.'
MAX_CACHE_SIZE = 20
_parse_cache = { }
def clear_cache():
'''Clear the parse cache.'''
_parse_cache.clear()
class ResultMixin(object):
'''Shared methods for the parsed result objects.'''
def username(self):
netloc = self.netloc
if '@' in netloc:
userinfo = netloc.rsplit('@', 1)[0]
if ':' in userinfo:
userinfo = userinfo.split(':', 1)[0]
return userinfo
username = property(username)
def password(self):
netloc = self.netloc
if '@' in netloc:
userinfo = netloc.rsplit('@', 1)[0]
if ':' in userinfo:
return userinfo.split(':', 1)[1]
password = property(password)
def hostname(self):
netloc = self.netloc.split('@')[-1]
if '[' in netloc and ']' in netloc:
return netloc.split(']')[0][1:].lower()
if None in netloc:
return netloc.split(':')[0].lower()
if None == '':
return None
return None.lower()
hostname = property(hostname)
def port(self):
netloc = self.netloc.split('@')[-1].split(']')[-1]
if ':' in netloc:
port = netloc.split(':')[1]
port = int(port, 10)
if port <= port:
pass
elif port <= 65535:
return port
port = property(port)
from collections import namedtuple
class SplitResult(namedtuple('SplitResult', 'scheme netloc path query fragment'), ResultMixin):
__slots__ = ()
def geturl(self):
return urlunsplit(self)
class ParseResult(namedtuple('ParseResult', 'scheme netloc path params query fragment'), ResultMixin):
__slots__ = ()
def geturl(self):
return urlunparse(self)
def urlparse(url, scheme = '', allow_fragments = True):
"""Parse a URL into 6 components:
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
Note that we don't break the components up in smaller bits
(e.g. netloc is a single string) and we don't expand % escapes."""
tuple = urlsplit(url, scheme, allow_fragments)
(scheme, netloc, url, query, fragment) = tuple
if scheme in uses_params and ';' in url:
(url, params) = _splitparams(url)
else:
params = ''
return ParseResult(scheme, netloc, url, params, query, fragment)
def _splitparams(url):
if '/' in url:
i = url.find(';', url.rfind('/'))
if i < 0:
return (url, '')
i = url.find(';')
return (url[:i], url[i + 1:])
def _splitnetloc(url, start = 0):
delim = len(url)
for c in '/?#':
wdelim = url.find(c, start)
if wdelim >= 0:
delim = min(delim, wdelim)
continue
return (url[start:delim], url[delim:])
def urlsplit(url, scheme = '', allow_fragments = True):
"""Parse a URL into 5 components:
<scheme>://<netloc>/<path>?<query>#<fragment>
Return a 5-tuple: (scheme, netloc, path, query, fragment).
Note that we don't break the components up in smaller bits
(e.g. netloc is a single string) and we don't expand % escapes."""
allow_fragments = bool(allow_fragments)
key = (url, scheme, allow_fragments, type(url), type(scheme))
cached = _parse_cache.get(key, None)
if cached:
return cached
if None(_parse_cache) >= MAX_CACHE_SIZE:
clear_cache()
netloc = query = fragment = ''
i = url.find(':')
if i > 0:
if url[:i] == 'http':
scheme = url[:i].lower()
url = url[i + 1:]
if url[:2] == '//':
(netloc, url) = _splitnetloc(url, 2)
if ('[' in netloc or ']' not in netloc or ']' in netloc) and '[' not in netloc:
raise ValueError('Invalid IPv6 URL')
if allow_fragments and '#' in url:
(url, fragment) = url.split('#', 1)
if '?' in url:
(url, query) = url.split('?', 1)
v = SplitResult(scheme, netloc, url, query, fragment)
_parse_cache[key] = v
return v
rest = url[i + 1:]
if not rest or any((lambda .0: pass)(rest)):
scheme = url[:i].lower()
url = rest
if url[:2] == '//':
(netloc, url) = _splitnetloc(url, 2)
if ('[' in netloc or ']' not in netloc or ']' in netloc) and '[' not in netloc:
raise ValueError('Invalid IPv6 URL')
if allow_fragments and '#' in url:
(url, fragment) = url.split('#', 1)
if '?' in url:
(url, query) = url.split('?', 1)
v = SplitResult(scheme, netloc, url, query, fragment)
_parse_cache[key] = v
return v
def urlunparse(data):
'''Put a parsed URL back together again. This may result in a
slightly different, but equivalent URL, if the URL that was parsed
originally had redundant delimiters, e.g. a ? with an empty query
(the draft states that these are equivalent).'''
(scheme, netloc, url, params, query, fragment) = data
if params:
url = '%s;%s' % (url, params)
return urlunsplit((scheme, netloc, url, query, fragment))
def urlunsplit(data):
'''Combine the elements of a tuple as returned by urlsplit() into a
complete URL as a string. The data argument can be any five-item iterable.
This may result in a slightly different, but equivalent URL, if the URL that
was parsed originally had unnecessary delimiters (for example, a ? with an
empty query; the RFC states that these are equivalent).'''
(scheme, netloc, url, query, fragment) = data
if (netloc or scheme) and scheme in uses_netloc and url[:2] != '//':
url = None + '//' if url and url[:1] != '/' else '' + url
if scheme:
url = scheme + ':' + url
if query:
url = url + '?' + query
if fragment:
url = url + '#' + fragment
return url
def urljoin(base, url, allow_fragments = True):
'''Join a base URL and a possibly relative URL to form an absolute
interpretation of the latter.'''
if not base:
return url
if not None:
return base
(bscheme, bnetloc, bpath, bparams, bquery, bfragment) = None(base, '', allow_fragments)
(scheme, netloc, path, params, query, fragment) = urlparse(url, bscheme, allow_fragments)
if scheme != bscheme or scheme not in uses_relative:
return url
if None in uses_netloc:
if netloc:
return urlunparse((scheme, netloc, path, params, query, fragment))
netloc = None
if path[:1] == '/':
return urlunparse((scheme, netloc, path, params, query, fragment))
if not None and not params:
path = bpath
params = bparams
if not query:
query = bquery
return urlunparse((scheme, netloc, path, params, query, fragment))
segments = None.split('/')[:-1] + path.split('/')
if segments[-1] == '.':
segments[-1] = ''
while '.' in segments:
segments.remove('.')
while None:
i = 1
n = len(segments) - 1
while i < n:
if segments[i] == '..' and segments[i - 1] not in ('', '..'):
del segments[i - 1:i + 1]
break
i = i + 1
break
continue
if segments == [
'',
'..']:
segments[-1] = ''
elif len(segments) >= 2 and segments[-1] == '..':
segments[-2:] = [
'']
return urlunparse((scheme, netloc, '/'.join(segments), params, query, fragment))
def urldefrag(url):
'''Removes any existing fragment from URL.
Returns a tuple of the defragmented URL and the fragment. If
the URL contained no fragments, the second element is the
empty string.
'''
if '#' in url:
(s, n, p, a, q, frag) = urlparse(url)
defrag = urlunparse((s, n, p, a, q, ''))
return (defrag, frag)
return (None, '')
try:
unicode
except NameError:
def _is_unicode(x):
return 0
def _is_unicode(x):
return isinstance(x, unicode)
_hexdig = '0123456789ABCDEFabcdef'
_hextochr = dict((lambda .0: pass)(_hexdig))
_asciire = re.compile('([\x00-\x7f]+)')
def unquote(s):
"""unquote('abc%20def') -> 'abc def'."""
if _is_unicode(s):
if '%' not in s:
return s
bits = None.split(s)
res = [
bits[0]]
append = res.append
for i in range(1, len(bits), 2):
append(unquote(str(bits[i])).decode('latin1'))
append(bits[i + 1])
return ''.join(res)
bits = None.split('%')
if len(bits) == 1:
return s
res = [
None[0]]
append = res.append
for item in bits[1:]:
try:
append(_hextochr[item[:2]])
append(item[2:])
continue
except KeyError:
append('%')
append(item)
continue
return ''.join(res)
def parse_qs(qs, keep_blank_values = 0, strict_parsing = 0):
'''Parse a query given as a string argument.
Arguments:
qs: percent-encoded query string to be parsed
keep_blank_values: flag indicating whether blank values in
percent-encoded queries should be treated as blank strings.
A true value indicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
strict_parsing: flag indicating what to do with parsing errors.
If false (the default), errors are silently ignored.
If true, errors raise a ValueError exception.
'''
dict = { }
for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
if name in dict:
dict[name].append(value)
continue
dict[name] = [
value]
return dict
def parse_qsl(qs, keep_blank_values = 0, strict_parsing = 0):
'''Parse a query given as a string argument.
Arguments:
qs: percent-encoded query string to be parsed
keep_blank_values: flag indicating whether blank values in
percent-encoded queries should be treated as blank strings. A
true value indicates that blanks should be retained as blank
strings. The default false value indicates that blank values
are to be ignored and treated as if they were not included.
strict_parsing: flag indicating what to do with parsing errors. If
false (the default), errors are silently ignored. If true,
errors raise a ValueError exception.
Returns a list, as G-d intended.
'''
pairs = [ s2 for s1 in qs.split('&') for s2 in s1.split(';') ]
r = []
for name_value in pairs:
if not name_value and not strict_parsing:
continue
nv = name_value.split('=', 1)
if len(nv) != 2:
if strict_parsing:
raise ValueError, 'bad query field: %r' % (name_value,)
if keep_blank_values:
nv.append('')
if not len(nv[1]):
if keep_blank_values:
name = unquote(nv[0].replace('+', ' '))
value = unquote(nv[1].replace('+', ' '))
r.append((name, value))
continue
return r